<template>
	<view class="work-container">
		<!-- 轮播图 -->
		<uni-swiper-dot class="uni-swiper-dot-box" :info="data" :current="current" field="content">
			<swiper class="swiper-box" :current="swiperDotIndex" @change="changeSwiper">
				<swiper-item v-for="(item, index) in data" :key="index">
					<view class="swiper-item" @click="clickBannerItem(item)">
						<image :src="item.image" mode="aspectFill" :draggable="false" />
					</view>
				</swiper-item>
			</swiper>
		</uni-swiper-dot>

		<template v-if="groups.length > 0">
		<uni-section :title="groups[0].name" type="line"></uni-section>
		<view class="grid-body">
			<uni-grid :column="4" :showBorder="false" @change="changeGrid0">
				<uni-grid-item v-for="(item,j) in groups[0].apps" :key="j" :index="j">
					<view class="grid-item-box">
						<uni-icons :type="item.icon" size="30"></uni-icons>
						<text class="text">{{item.name}}</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</view>
		</template>
		
		<template v-if="groups.length > 1">
		<uni-section :title="groups[1].name" type="line"></uni-section>
		<view class="grid-body">
			<uni-grid :column="4" :showBorder="false" @change="changeGrid1">
				<uni-grid-item v-for="(item,j) in groups[1].apps" :key="j" :index="j">
					<view class="grid-item-box">
						<uni-icons :type="item.icon" size="30"></uni-icons>
						<text class="text">{{item.name}}</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</view>
		</template>
		
		<template v-if="groups.length > 2">
		<uni-section :title="groups[2].name" type="line"></uni-section>
		<view class="grid-body">
			<uni-grid :column="4" :showBorder="false" @change="changeGrid2">
				<uni-grid-item v-for="(item,j) in groups[2].apps" :key="j" :index="j">
					<view class="grid-item-box">
						<uni-icons :type="item.icon" size="30"></uni-icons>
						<text class="text">{{item.name}}</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</view>
		</template>
		
		<template v-if="groups.length > 3">
		<uni-section :title="groups[3].name" type="line"></uni-section>
		<view class="grid-body">
			<uni-grid :column="4" :showBorder="false" @change="changeGrid3">
				<uni-grid-item v-for="(item,j) in groups[3].apps" :key="j" :index="j">
					<view class="grid-item-box">
						<uni-icons :type="item.icon" size="30"></uni-icons>
						<text class="text">{{item.name}}</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</view>
		</template>
		
		<template v-if="groups.length > 4">
		<uni-section :title="groups[4].name" type="line"></uni-section>
		<view class="grid-body">
			<uni-grid :column="4" :showBorder="false" @change="changeGrid4">
				<uni-grid-item v-for="(item,j) in groups[4].apps" :key="j" :index="j">
					<view class="grid-item-box">
						<uni-icons :type="item.icon" size="30"></uni-icons>
						<text class="text">{{item.name}}</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</view>
		</template>			
		
	</view>
</template>

<script>
	import { apiGet	} from '@/api/openapi.js'
	import { getApps	} from '@/api/app.js'
	export default {
		data() {
			return {
				current: 0,
				swiperDotIndex: 0,
				data: [{
						image: '/static/images/banner/banner01.jpg'
					},
					{
						image: '/static/images/banner/banner02.jpg'
					},
					{
						image: '/static/images/banner/banner03.jpg'
					}
				],
				groups:[]
			}
		},
		onLoad() {
			this.loadGroups();
		},
		methods: {
			loadGroups(){
				apiGet({"@table":"dev_group","@pager":{order:"sorts ASC", limit:1000}}).then(response=>{
					let groups = [];
					response.data.rows.forEach(e=>{
						let group = {
							id:e.id,
							name:e.name,
							sorts:e.sorts,
							icon:e.icon,
							apps:[]
						};
						groups.push(group);
								
					});
					groups.sort((a,b) => a.sorts - b.sorts);
					this.groups = groups;
					this.loadApps();
				});
			},
			loadApps() {
				getApps().then(response=>{
					let groups = this.groups;
					response.data.rows.forEach(e=>{
						for(let i=0;i<groups.length;i++){
							if(groups[i].id == e.group_id){
								groups[i].apps.push(e);
							}
						}
					});
					
					groups.forEach(g=>{
						g.apps.sort((a,b) => a.sorts - b.sorts);
					});
					this.groups = groups;
				});
			},
			clickBannerItem(item) {
				console.info(item)
			},
			changeSwiper(e) {
				this.current = e.detail.current
			},
			changeGrid0(e) {
				this.gotoApp(this.groups[0]['apps'][e.detail.index]);
			},
			changeGrid1(e) {
				this.gotoApp(this.groups[1]['apps'][e.detail.index]);
			},
			changeGrid2(e) {
				this.gotoApp(this.groups[2]['apps'][e.detail.index]);
			},
			changeGrid3(e) {
				this.gotoApp(this.groups[3]['apps'][e.detail.index]);
			},
			changeGrid4(e) {
				this.gotoApp(this.groups[4]['apps'][e.detail.index]);
			},
			gotoApp(app){
				let url = '/pages/dev/list?app='+app.code;
				if(app.type == 'form'){
					url = '/pages/dev/form?app='+app.code;
				}else if(app.type == 'flow'){
					url = '/pages/dev/flow/index?app='+app.code;
				}else if(app.type == 'page'){
					url = '/pages/dev/page?app='+app.code;
				}else if(app.type == 'local'){
					url = app.links;
				}
				uni.navigateTo({
					url: url
				});
			}
		}
	}
</script>

<style lang="scss">
	/* #ifndef APP-NVUE */
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #fff;
		min-height: 100%;
		height: auto;
	}

	view {
		font-size: 14px;
		line-height: inherit;
	}

	/* #endif */

	.text {
		text-align: center;
		font-size: 26rpx;
		margin-top: 10rpx;
	}

	.grid-item-box {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 15px 0;
	}

	.uni-margin-wrap {
		width: 690rpx;
		width: 100%;
		;
	}

	.swiper {
		height: 300rpx;
	}

	.swiper-box {
		height: 150px;
	}

	.swiper-item {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		align-items: center;
		color: #fff;
		height: 300rpx;
		line-height: 300rpx;
	}

	@media screen and (min-width: 500px) {
		.uni-swiper-dot-box {
			width: 400px;
			/* #ifndef APP-NVUE */
			margin: 0 auto;
			/* #endif */
			margin-top: 8px;
		}

		.image {
			width: 100%;
		}
	}
</style>